package com.xky.sportstestdemo01.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xky.sportstestdemo01.common.ScoreCalculator;
import com.xky.sportstestdemo01.mapper.PhysicalFitnessMapper;
import com.xky.sportstestdemo01.mapper.PhysicalFitnessScoresMapper;
import com.xky.sportstestdemo01.mapper.StudentMapper;
import com.xky.sportstestdemo01.pojo.entity.PhysicalFitness;
import com.xky.sportstestdemo01.pojo.entity.PhysicalFitnessScores;
import com.xky.sportstestdemo01.pojo.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 体测成绩表(PhysicalFitness)表服务实现类
 *
 * @author twt
 * @since 2024-11-08 13:49:41
 */
@Service("physicalFitnessService")
public class PhysicalFitnessService extends ServiceImpl<PhysicalFitnessMapper, PhysicalFitness> {

    @Autowired
    private PhysicalFitnessMapper physicalFitnessMapper;

    @Autowired
    private PhysicalFitnessScoresMapper physicalFitnessScoresMapper;

    @Autowired
    private StudentMapper studentMapper;

    /**
     * @auther: 唐文涛
     * 根据学生id判断性别
     */
    public boolean isMale(Integer studentId) {
        // 查询Student表中的数据
        Student student = studentMapper.selectById(studentId);
        //TODO——触发警报
        if (student == null) {
            return false;
        }
        return student.getGender() == 1;
    }

    /**
     * @auther: 唐文涛
     * 根据学生id判断大一大二还是大三大四
     */
    public boolean isOneTwo(Integer studentId) {
        // 查询Student表中的数据
        Student student = studentMapper.selectById(studentId);
        //TODO——触发警报
        if (student == null) {
            return false;
        }
        return student.getGradeNumber().equals("41") || student.getGradeNumber().equals("42");
    }

    /**
     * @param studentId 学生id
     * @return 体测成绩
     * @auther: 唐文涛
     * 根据学生id计算体测成绩
     */
    public PhysicalFitnessScores calculateScores(Integer studentId) {
        // 查询physical_fitness表中的数据
        PhysicalFitness physicalFitness = physicalFitnessMapper.selectById(studentId);
        if (physicalFitness == null) {
            return null;
        }
        PhysicalFitnessScores scores = new PhysicalFitnessScores();
        // 根据业务逻辑计算分数

        // 使用ScoreCalculator计算各项成绩
        scores.setLungCapacityScore(ScoreCalculator.calculateLungCapacityScore(physicalFitness.getLungCapacity(), isMale(studentId), isOneTwo(studentId)));
        scores.setFiftyMeterRunScore(ScoreCalculator.calculateFiftyMeterRunScore(physicalFitness.getFiftyMeterRun(), isMale(studentId), isOneTwo(studentId)));
        scores.setStandingLongJumpScore(ScoreCalculator.calculateStandingLongJumpScore(physicalFitness.getStandingLongJump(), isMale(studentId), isOneTwo(studentId)));
        scores.setSitAndReachScore(ScoreCalculator.calculateSitAndReachScore(physicalFitness.getSitAndReach(), isMale(studentId), isOneTwo(studentId)));
        scores.setOneMinuteSitUpsScore(ScoreCalculator.calculateOneMinuteSitUpsScore(physicalFitness.getOneMinuteSitUps(), isMale(studentId), isOneTwo(studentId)));
        scores.setPullUpsScore(ScoreCalculator.calculatePullUpsScore(physicalFitness.getPullUps(), isMale(studentId), isOneTwo(studentId)));
        scores.setEightHundredMeterRunScore(ScoreCalculator.calculateEightHundredMeterRunScore(physicalFitness.getEightHundredMeterRun(), isMale(studentId), isOneTwo(studentId)));
        scores.setOneThousandMeterRunScore(ScoreCalculator.calculateOneThousandMeterRunScore(physicalFitness.getOneThousandMeterRun(), isMale(studentId), isOneTwo(studentId)));


        // 计算总分
        double totalScore = scores.getLungCapacityScore() +
                scores.getFiftyMeterRunScore() +
                scores.getStandingLongJumpScore() +
                scores.getSitAndReachScore() +
                scores.getEightHundredMeterRunScore() +
                scores.getOneThousandMeterRunScore() +
                scores.getOneMinuteSitUpsScore() +
                scores.getPullUpsScore();

        scores.setTotalScore(totalScore);

        // 保存分数到physical_fitness_scores表
        physicalFitnessScoresMapper.insert(scores);

        return scores;
    }
}